package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.organization.util.Voucher;
import com.turing.post.organization.util.VoucherDao;
import com.turing.post.organization.util.VoucherInStockOrder;
import com.turing.post.organization.util.VoucherInStockOrderBusiness;
import com.turing.post.organization.util.VoucherInStockOrderDao;
import com.turing.post.organization.util.VoucherInStockOrderDetail;
import com.turing.post.organization.util.VoucherInStockOrderDetailBusiness;
import com.turing.post.organization.util.VoucherInStockOrderDetailDao;
import com.turing.post.organization.util.VoucherStock;
import com.turing.post.organization.util.VoucherStockBusiness;
import com.turing.post.organization.util.VoucherStockDao;
import com.turing.post.organization.util.VoucherStockDetail;
import com.turing.post.organization.util.VoucherStockDetailBusiness;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.VoucherInStockOrderBusinessImpl")
@Remote
@Local
public class VoucherInStockOrderBusinessImpl implements
		VoucherInStockOrderBusiness {

	@EJB(mappedName = "organization.impl.VoucherInStockOrderDaoImpl")
	private VoucherInStockOrderDao voucherInStockOrderDaoImpl;// 凭证入库单

	@EJB(mappedName = "organization.impl.VoucherStockBusinessImpl")
	private VoucherStockBusiness voucherStockBusinessDaoImpl;// 凭证库存业务

	@EJB(mappedName = "organization.impl.VoucherStockDetailBusinessImpl")
	private VoucherStockDetailBusiness voucherStockDetailBusinessDaoImpl;// 凭证库存详细业务

	@EJB(mappedName = "organization.impl.VoucherInStockOrderDetailBusinessImpl")
	private VoucherInStockOrderDetailBusiness voucherInStockOrderDetailBusinessImpl;// 凭证入库单明细业务

	@EJB(mappedName = "organization.impl.VoucherInStockOrderDetailDaoImpl")
	private VoucherInStockOrderDetailDao voucherInStockOrderDetailDaoImpl;// 凭证入库单明细业务
	
//	@EJB(mappedName = "organization.impl.VoucherStockDetailDaoImpl")
//	private VoucherStockDetailDao voucherStockDetailDaoImpl;// 凭证入库单明细

	@EJB(mappedName = "organization.impl.VoucherStockDaoImpl")
	private VoucherStockDao voucherStockDaoImpl;// 凭证库存

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;// 主键

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;// 发卡商

	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao sysBranchDaoImpl;// 序号

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;// 日志

	@EJB(mappedName = "organization.impl.VoucherDaoImpl")
	private VoucherDao voucherDaoImpl;// 凭证种类表
	

	public BigInteger insertVoucherInStockOrder(PCPSWebInput webInput,
			VoucherInStockOrder voucherInStockOrder,
			List<VoucherInStockOrderDetail> datailList)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 操作员
		if (webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"操作员为空!");
		}
		// 验证机构
		SysBranch branch = sysBranchDaoImpl.getSysBranchByBranchNo(webInput
				.getOperBranchNo());
		if (branch == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"机构号有误!");
		}
		String branchtype = branch.getBranchType();
		if (!branchtype.equals(SysBranch.SYSBRANCH_ISSUEBRANCH)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"用户所在机构不是发卡机构!");
		}
		// 发卡商
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(webInput.getOperBranchNo()
						.substring(0, 6));
		Integer seqNo = sysBranchDaoImpl.updateSysBranchOrderSeqNo(webInput
				.getOperBranchNo());
		// 业务单号
		String orderSeqNo = webInput.getOperBranchNo()
				+ PCPSUtil.getPackedYMDString(ic.getTranDate())
				+ String.format("%05d", seqNo);
		if (PCPSUtil.isEmpty(orderSeqNo)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务单号为空!");
		}
		voucherInStockOrder.setIssueCompanyNo(webInput.getOperBranchNo()
				.substring(0, 6));
		voucherInStockOrder.setIssueBranchNo(webInput.getOperBranchNo());
		voucherInStockOrder.setOrderSeqNo(orderSeqNo);

		VoucherInStockOrderDetail insertDetail = null;
		List<VoucherInStockOrderDetail> insertList = new ArrayList<VoucherInStockOrderDetail>();
		// 凭证入库单明细
		int k=1;
		HashMap<String,String> startseqmap=new HashMap<String,String>();
		HashMap<String,String> endseqmap=new HashMap<String,String>();
		Voucher voucher =null;
		if(datailList==null || datailList.size()==0){
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"", "凭证号段为空!");
		}
		for (int i = 0; i < datailList.size(); i++) {
			insertDetail = (VoucherInStockOrderDetail) datailList.get(i);
			if (PCPSUtil.isEmpty(insertDetail.getStartVoucherNo())) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "起始序号为空!");
			}
			if (PCPSUtil.isEmpty(insertDetail.getEndVoucherNo())) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "终止序号为空!");
			}
            
			voucher=voucherDaoImpl.getVoucherByVoucherId(insertDetail
					.getVoucherId());
			if (voucher == null) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证种类不存在!");
			}
			if(startseqmap.get(insertDetail.getStartVoucherNo().trim())!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			
			if(endseqmap.get(insertDetail.getEndVoucherNo().trim())!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行终止号已经重复录入!");
			}
			
			if(endseqmap.get(insertDetail.getStartVoucherNo().trim())!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			if(startseqmap.get(insertDetail.getEndVoucherNo().trim())!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行终止号已经重复录入!");
			}
			if(isVoucherNo(insertDetail.getStartVoucherNo().trim(),startseqmap).endsWith("error")){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			if(isVoucherNo(insertDetail.getEndVoucherNo().trim(),endseqmap).endsWith("error")){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			startseqmap.put(insertDetail.getStartVoucherNo().trim(),insertDetail.getEndVoucherNo().trim());
			endseqmap.put(insertDetail.getEndVoucherNo().trim(), insertDetail.getStartVoucherNo().trim());
			
			
			BigInteger voucherNoLen = voucher.getVoucherNoLen();
			BigInteger templength = new BigInteger(insertDetail
					.getEndVoucherNo()).subtract(new BigInteger(insertDetail
					.getStartVoucherNo()));
			templength=templength.add(BigInteger.ONE);
			if (templength.intValue() < 0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "号段有误!");
			}
			if (insertDetail.getStartVoucherNo().length() != voucherNoLen
					.intValue()) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号长度应为: " + voucherNoLen.toString() + "位数字!");
			}
			if (insertDetail.getEndVoucherNo().length() != voucherNoLen
					.intValue()) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第"+k+"行终止号长度应为: " + voucherNoLen.toString() + "位数字!");
			}
			if (voucherInStockOrderDetailDaoImpl.IsSeqNo(webInput, insertDetail.getVoucherId(),
					insertDetail.getStartVoucherNo(),
					insertDetail.getEndVoucherNo()).intValue() > 0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第"+k+"行起始凭证顺序号已经存在!");
			}
			if (voucherStockDaoImpl.getVoucherStockListByStatusTotal(webInput,
					insertDetail.getVoucherId(),
					insertDetail.getStartVoucherNo(),
					insertDetail.getEndVoucherNo(), null).compareTo(
					BigInteger.ZERO) > 0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第"+k+"行起始凭证顺序号已经存在!");
			}            
			
			insertDetail.setSeqNo(new BigInteger(String.valueOf(i + 1)));
			insertList.add(insertDetail);
			k++;
		}

		// 得到入库单ID
		BigInteger voucherInStockOrderId = pcpsCommonDaoImpl
				.getUniqueIntegerKey();
		voucherInStockOrder.setVoucherInStockOrderId(voucherInStockOrderId);
		// 交易日期
		voucherInStockOrder.setTranDate(ic.getTranDate());
		// 入库时间
		voucherInStockOrder.setTranTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		// 操作员号
		voucherInStockOrder.setOperId(webInput.getOperId());
		// 状态
		voucherInStockOrder.setStatus(VoucherInStockOrder.Status_0);
		// 插入凭证入库单
		voucherInStockOrderDaoImpl
				.insertVoucherInStockOrder(voucherInStockOrder);

		for (VoucherInStockOrderDetail insert : insertList) {
			insert.setVoucherInStockOrderId(voucherInStockOrderId);
			voucherInStockOrderDetailBusinessImpl
					.insertVoucherInStockOrderDetail(webInput, insert);
		}
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "凭证入库单", voucherInStockOrder,
				SysOperLog.operType_Add, "OrderSeqNo:"
						+ voucherInStockOrder.getOrderSeqNo());
		return voucherInStockOrderId;
	}

	/**
	 * 修改凭证入库单状态
	 * 
	 * @param voucherInStockOrder
	 *            凭证入库单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherInStockOrderByStatus(PCPSWebInput webInput,
			VoucherInStockOrder voucherInStockOrder)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 验证入库单ID
		if (voucherInStockOrder.getVoucherInStockOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"入库单ID为空!");
		}
		// 验证操作人
		if (webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"用户ID为空!");
		}
		// 验证备注
		if (PCPSUtil.isEmpty(voucherInStockOrder.getRemark())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"备注为空!");
		}
		// 验证状态
		if (PCPSUtil.isEmpty(voucherInStockOrder.getStatus())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"状态为空!");
		}
		// 验证机构
		SysBranch branch = sysBranchDaoImpl.getSysBranchByBranchNo(webInput
				.getOperBranchNo());
		if (branch == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"机构号有误!");
		}
		String branchtype = branch.getBranchType();
		if (!branchtype.equals(SysBranch.SYSBRANCH_ISSUEBRANCH)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"用户所在机构不是发卡机构!");
		}
		// 验证入库单是否存在
		VoucherInStockOrder voucherInStockOrder1 = voucherInStockOrderDaoImpl
				.getVoucherInStockOrdeVoucherId(voucherInStockOrder
						.getVoucherInStockOrderId());
		if (voucherInStockOrder1 == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证入库单不存在!");
		}
		String status = voucherInStockOrder.getStatus();
		String status1 = voucherInStockOrder1.getStatus();
		if (status1.equals(VoucherInStockOrder.Status_0)) {// 入库审核
			if (voucherInStockOrder1.getOperId().equals(webInput.getOperId())) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "审核操作不能同一人!请合理分配角色!");
			}
			if (!status.equals(VoucherInStockOrder.Status_1)) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证入库单状态无效!不能做入库审核操作!");
			} else {// 入库
				voucherInStockOrder1.setStatus(VoucherInStockOrder.Status_1);
				voucherInStockOrder1.setAuthOperId(webInput.getOperId());
				voucherInStockOrder1.setAuthTime(pcpsCommonDaoImpl
						.getDatabaseSysTime());
				voucherInStockOrder1.setRemark(voucherInStockOrder.getRemark());
				voucherStockBusinessDaoImpl.insertVoucherStock(webInput,
						voucherInStockOrder.getVoucherInStockOrderId());
				voucherInStockOrderDaoImpl
						.updateVoucherInStockOrder(voucherInStockOrder1);
				
				List<VoucherInStockOrderDetail> datillist = voucherInStockOrderDetailDaoImpl
				.getVoucherInStockOrderDetailByVoucherInStockOrderIdNotPage(
						webInput, voucherInStockOrder1
								.getVoucherInStockOrderId());
				if(datillist==null && datillist.size()==0){
					 throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
							"", "凭证入库明细不存在!");
				}
				VoucherStockDetail voucherStockDetail = null;
				VoucherInStockOrderDetail indatil =null;
				for (int i = 0; i < datillist.size(); i++) {
					indatil = (VoucherInStockOrderDetail) datillist
							.get(i);
					
					// 凭证出入库明细
					voucherStockDetail = new VoucherStockDetail();
					voucherStockDetail.setIoOrderId(voucherInStockOrder1.getVoucherInStockOrderId());
					voucherStockDetail.setBranchNo(voucherInStockOrder1.getIssueBranchNo());
					voucherStockDetail.setTranDate(voucherInStockOrder1.getTranDate());
					voucherStockDetail.setTranTime(pcpsCommonDaoImpl.getDatabaseSysTime());
					voucherStockDetail.setIssueCompanyNo(voucherInStockOrder1.getIssueCompanyNo());
					voucherStockDetail.setVoucherId(indatil.getVoucherId());
					voucherStockDetail.setStartVoucherNo(indatil.getStartVoucherNo());
					voucherStockDetail.setEndVoucherNo(indatil.getEndVoucherNo());
					voucherStockDetail.setCount(indatil.getCount());
					voucherStockDetail.setTranType(VoucherStockDetail.Status_0);
					voucherStockDetail.setOperId(webInput.getOperId());
					voucherStockDetailBusinessDaoImpl.insertVoucherStockDetail(webInput, voucherStockDetail);
				}
			}
		} else if (status1.equals(VoucherInStockOrder.Status_2)) {// 撤销确认审核
			if (!status.equals(VoucherInStockOrder.Status_3)) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证入库单状态无效!不能做入库撤销操作!");
			} else {// 入库撤销
				voucherInStockOrder1.setStatus(VoucherInStockOrder.Status_3);
				voucherInStockOrder1.setCancelInStockTime(pcpsCommonDaoImpl
						.getDatabaseSysTime());
				voucherInStockOrder1.setAuthOperId(webInput.getOperId());
				if (voucherInStockOrder1.getOperId().equals(
						webInput.getOperId())) {
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "审核人和撤销人不能一致!请合理分配角色!");
				}
				if (!voucherInStockOrder1.getIssueBranchNo().equals(
						webInput.getOperBranchNo())) {
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "撤销人机构和创建人机构不一致!请合理分配角色!");
				}
				List<VoucherInStockOrderDetail> datillist = voucherInStockOrderDetailDaoImpl
						.getVoucherInStockOrderDetailByVoucherInStockOrderIdNotPage(
								webInput, voucherInStockOrder1
										.getVoucherInStockOrderId());
				for (int i = 0; i < datillist.size(); i++) {// 验证号段是否有误
					VoucherInStockOrderDetail indatil = (VoucherInStockOrderDetail) datillist
							.get(i);
					BigInteger stockcount = voucherStockDaoImpl
							.getVoucherStockListByBranchNoTotal(
									voucherInStockOrder1.getIssueBranchNo(),
									indatil.getVoucherId(), indatil
											.getStartVoucherNo(), indatil
											.getEndVoucherNo());
					if (stockcount.intValue() == 0
							|| indatil.getCount() != stockcount.intValue()) {
						throw new OrganApplicationException(
								OrganRespCode.RC_ARGS_NULL, "",
								"撤销凭证单号库存有误!凭证号已经不在本机构!");
					}
				}
				VoucherStockDetail voucherStockDetail = null;
				VoucherInStockOrderDetail indatil =null;
				for (int i = 0; i < datillist.size(); i++) {
					indatil = (VoucherInStockOrderDetail) datillist
							.get(i);
					// 修改凭证库存
					voucherStockBusinessDaoImpl.deleteVoucherStock(webInput,
							indatil);
					//凭证出入库明细
					voucherStockDetail = new VoucherStockDetail();
					voucherStockDetail.setIoOrderId(voucherInStockOrder1.getVoucherInStockOrderId());
					voucherStockDetail.setBranchNo(voucherInStockOrder1.getIssueBranchNo());
					voucherStockDetail.setTranDate(voucherInStockOrder1.getTranDate());
					voucherStockDetail.setTranTime(pcpsCommonDaoImpl.getDatabaseSysTime());
					voucherStockDetail.setIssueCompanyNo(voucherInStockOrder1.getIssueCompanyNo());
					voucherStockDetail.setVoucherId(indatil.getVoucherId());
					voucherStockDetail.setStartVoucherNo(indatil.getStartVoucherNo());
					voucherStockDetail.setEndVoucherNo(indatil.getEndVoucherNo());
					voucherStockDetail.setCount(-indatil.getCount());
					voucherStockDetail.setTranType(VoucherStockDetail.Status_C);
					voucherStockDetail.setOperId(webInput.getOperId());
					voucherStockDetailBusinessDaoImpl.insertVoucherStockDetail(webInput, voucherStockDetail);
				}
				//voucherStockDetailDaoImpl.updateVoucherStockDetail(voucherInStockOrder.getVoucherInStockOrderId());
			}
			voucherInStockOrder1.setRemark(voucherInStockOrder.getRemark());
			// 修改
			voucherInStockOrderDaoImpl
					.updateVoucherInStockOrder(voucherInStockOrder1);
		} else if (status1.equals(VoucherInStockOrder.Status_1)) {// 撤销申请
			if (!status.equals(VoucherInStockOrder.Status_2)) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证入库单状态无效!不能做入库撤销申请操作!");
			} else {// 入库撤销待审			
				
				voucherInStockOrder1.setStatus(VoucherInStockOrder.Status_2);		
				if (voucherInStockOrder1.getOperId().compareTo(webInput.getOperId())!=0){
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "撤销申请人和创建人不一致!请合理分配角色!");
				}
				if (!voucherInStockOrder1.getIssueBranchNo().equals(
						webInput.getOperBranchNo())) {
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "撤销人机构和创建人机构不一致!请合理分配角色!");
				}
				// 出入库明细：
				List<VoucherInStockOrderDetail> datillist = voucherInStockOrderDetailDaoImpl
						.getVoucherInStockOrderDetailByVoucherInStockOrderIdNotPage(
								webInput, voucherInStockOrder1
										.getVoucherInStockOrderId());
				if (datillist == null || datillist.size() <= 0) {
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "入库撤销单有误!入库明细有误!");
				}
				VoucherInStockOrderDetail indatil = null;
				for (int i = 0; i < datillist.size(); i++) {// 验证号段是否有误
					indatil = (VoucherInStockOrderDetail) datillist
							.get(i);
					BigInteger stockcount = voucherStockDaoImpl
							.getVoucherStockListByBranchNoTotal(
									voucherInStockOrder1.getIssueBranchNo(),
									indatil.getVoucherId(), indatil
											.getStartVoucherNo(), indatil
											.getEndVoucherNo());
					if (stockcount.intValue() == 0
							|| indatil.getCount() != stockcount.intValue()) {
						throw new OrganApplicationException(
								OrganRespCode.RC_ARGS_NULL, "",
								"撤销凭证单号库存有误!凭证号应经不在本机构!");
					}
					//验证撤销申请的序号是否有效
					if (voucherStockDaoImpl.getVoucherStockListByStatusTotal(webInput,
							indatil.getVoucherId(),
							indatil.getStartVoucherNo().trim(),
							indatil.getEndVoucherNo().trim(), VoucherStock.Status_0).intValue()!=indatil.getCount()){
						throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
								"", "撤销的凭证号,已不是正常状态,不能取消!");
					} 
				}
			}
			voucherInStockOrder1.setRemark(voucherInStockOrder.getRemark());
			// 修改
			voucherInStockOrderDaoImpl
					.updateVoucherInStockOrder(voucherInStockOrder1);
		}
		// 修改数据库操作日志
		insertRecordingOperationLog(webInput, "凭证入库单", voucherInStockOrder,
				SysOperLog.operType_Edit, "OrderSeqNo:"
						+ voucherInStockOrder.getOrderSeqNo());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherInStockOrder obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
   /**
    * 验证号段是否重复
    */
	@SuppressWarnings("unchecked")
	private String isVoucherNo(String Voucherno,Map map){
		Iterator ite = map.entrySet().iterator();	
		while(ite.hasNext()){		
			Map.Entry<Object, Object> entry = (Entry<Object, Object>) ite.next();	
			Object key = entry.getKey();	
			Object value = entry.getValue();
			BigInteger bigkey=new BigInteger(key.toString());
			BigInteger bigvalue=new BigInteger(value.toString());
			BigInteger bigvoucherno=new BigInteger(Voucherno);
			if(bigvoucherno.intValue()>=bigkey.intValue() && bigvoucherno.intValue()<=bigvalue.intValue())
				return "error";
		}
		return "";
	}
}
